<template>
  <div>
    <div class="nav-bar">
      <div>
        <div class="left" @click="back">
          <img src="../../../assets/goleft.svg" alt="" />
        </div>
      </div>

      <div class="center">专注计时</div>
      <div class="right">
        <!-- <img src="../../../assets/add.svg" alt="" /> -->
      </div>
    </div>
    <div class="time">
      <!-- <button @click="start">计时开始</button> -->
      <div class="timer-div dowebok">
        <div class="timer">
          <div class="time-card" data-type="hours" data-max="24">
            <div class="time-card-count">{{ myHours }}}</div>
            <div class="time-card-label">时</div>
          </div>
          <span class="colon">:</span>
          <div class="time-card" data-type="minutes" data-max="60">
            <div class="time-card-count">{{ myMinutes }}</div>
            <div class="time-card-label">分</div>
          </div>
          <span class="colon">:</span>
          <div class="time-card" data-type="seconds" data-max="60">
            <div class="time-card-count">{{ mySeconds }}</div>
            <div class="time-card-label">秒</div>
          </div>
        </div>
      </div>
      <button class="timeBtn" @click="start">开始计时</button>
      <button class="stopBtn" disabled="disabled">想暂停? 请专注!</button>
      <button class="restartBtn" @click="reStart">重置</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "navbar",
  data() {
    return {
      // popupVisible: false,
      currentTime: 0,
      timeObj: null, // 时间对象,下方会用到
      myHours: "00", // 我定义来接收计算出来的 小时 的
      myMinutes: "00", // 我定义来接收计算出来的 分钟 的
      mySeconds: "00", // 我定义来接收计算出来的 秒钟 的
      isStart: false,
      stop:false,
    };
  },

  methods: {
    back() {
      this.$router.replace("/home");
    },
    start() {
      this.stop=false
      if (this.isStart == false) {
        console.log("sdf");
        this.timeFunction();
        setInterval(() => {
          this.timeFunction();
        }, 1000);
      }
      this.isStart=true;
    },
    reStart(){
      this.currentTime= 0
      this.timeObj=null
      this.myHours= "00"
      this.myMinutes= "00"
      this.mySeconds= "00"
      this.stop=true
    },
    timeFunction() {
      if(this.stop==false){
      // 开始执行倒计时
      this.timeObj = {
        // 时间对象
        seconds: Math.floor(this.currentTime % 60),
        minutes: Math.floor(this.currentTime / 60) % 60,
        hours: Math.floor(this.currentTime / 60 / 60) % 24,
      };
      // 计算出时分秒
      this.myHours =
        this.timeObj.hours < 10 ? "0" + this.timeObj.hours : this.timeObj.hours;
      this.myMinutes =
        this.timeObj.minutes < 10
          ? "0" + this.timeObj.minutes
          : this.timeObj.minutes;
      this.mySeconds =
        this.timeObj.seconds < 10
          ? "0" + this.timeObj.seconds
          : this.timeObj.seconds;
      this.currentTime++;
      }
    },
  },
};
</script>

<style scoped>
.nav-bar {
  display: flex;
  height: 44px;
  line-height: 44px;
  text-align: center;
  box-shadow: 0 1px 1px rgba(100, 100, 100, 0.301);
  font-size: 18px;
  font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
  box-shadow: darkgray;
  /* background-color: coral; */
}
.left,
.right {
  width: 60px;
  height: 44px;
  line-height: 44px;
}
.left img,
.right img {
  width: 24px;
  height: 24px;
  margin-top: 10px;
}
.center {
  flex: 1;
}
.time {
  width: 100%;
  height: 80%;
}
.timeBtn {
  width: 66%;
  height: 50px;
  border-radius: 20px;
  background-color: #65a3f5;
  margin-left: 17%;
  margin-top: 30px;
}
.stopBtn {
  width: 66%;
  height: 50px;
  border-radius: 20px;
  background-color: #f5657c;
  margin-left: 17%;
  margin-top: 10px;
}
.restartBtn {
  width: 66%;
  height: 50px;
  border-radius: 20px;
  /* background-color: #f5657c; */
  margin-left: 17%;
  margin-top: 10px;
}
.dowebok {
  margin-top: 200px;
  align-items: center;
}
.timer {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin: 0 0 4em;
  color: #151414;
}
.time-card {
  margin: 0 1em;
  text-align: center;
}
.time-card-count {
  font-weight: 600;
  font-size: 70px;
  width: 83px;
  height: 77px;
  line-height: 77px;
  overflow: hidden;
}
.time-card-label {
  font-size: 0.625em;
  text-transform: uppercase;
  opacity: 0.7;
}
.colon {
  font-size: 2em;
}
</style>


